package org.beangle.notification.mail

import org.beangle.commons.lang.Assert
import org.beangle.notification.Message
import org.beangle.notification.NotificationException
import org.beangle.notification.Notifier
import org.beangle.commons.logging.Logging

abstract class AbstractMailNotifier[T <: MailMessage] extends Notifier[T] with Logging{

  protected var mailSender: MailSender = _

  
  var from: String = _

  def getType(): String = "mail"

  def deliver(msg: T) {
    beforeSend(msg)
    try {
      if (null == msg.from && null != from) msg.from(from)
      mailSender.send(msg)
      afterSend(msg)
    } catch {
      case e: NotificationException =>// logger.error("Cannot send message " + msg.getSubject, e)
    }
  }

  protected def buildSubject(msg: Message): String

  protected def buildText(msg: Message): String

  protected def beforeSend(msg: Message) {
  }

  protected def afterSend(msg: Message) {
  }
}
